<?php

class Tuijian {

    var $_member_mod;

    function __construct() {
        $this->_member_mod = & m('member');
    }

    function do_tuijian($order) {
        $mysql_flag=$this->tuijian_seller($order);
        if(!$mysql_flag){
            return false;
        }
        $mysql_flag=$this->tuijian_buyer($order);
        if(!$mysql_flag){
            return false;
        }
        return true;
    }

    /**
     * 此处是  推荐成为卖家， 卖家卖出产品后，他的推荐者可以获得佣金
     */
    function tuijian_seller($order) {
        $add_time = gmtime();
        
        //判断是否开启 推荐会员成为卖家 ， 卖家卖出产品  推荐者会获取提成
        if (!Conf::get('tuijian_seller_status')) {
            return true;
        }

        /* 第1级 佣金操作  查看卖家是否有推荐人  BEGIN */
        if (!Conf::get('tuijian_seller_ratio1')) {
            return true;
        }
        $tuijian_seller_ratio1 = round(Conf::get('tuijian_seller_ratio1') / 100, 2);

        //卖家相关信息
        $seller_id = $order['seller_id'];
        $seller_info = $this->_member_mod->get($seller_id);


        //1级推荐人 不存在卖家的推荐人则返回
        if (!$seller_info['referid']) {
            return true;
        }
        $referid_1 = $seller_info['referid'];
        //查看推荐人是否存在 不存在则不操作
        $referinfo_1 = $this->_member_mod->get($referid_1);
        if (empty($referinfo_1)) {
            return true;
        }
        if(round($order['goods_amount'] * $tuijian_seller_ratio1, 2)>0){
        //卖家先扣除佣金
        $mysql_flag=$this->_member_mod->change_money(
                array(
                    'user_id' => $seller_id,
                    'add_time'=>$add_time,
                    'user_name' => $seller_info['user_name'],
                    'order_sn' => $order['order_sn'],
                    'type' => ACCOUNT_TYPE_TUIJIAN_SELLER,
                    'money' => round($order['goods_amount'] * $tuijian_seller_ratio1, 2), #一级推荐人应该获取的佣金
                    'money_flow' => 'outlay', #流出佣金
                    'complete' => '1',
                    'log_text' => '恭喜你缴纳' . round($order['goods_amount'] * $tuijian_seller_ratio1, 2) . '元佣金,佣金被1级店铺推荐者'
                    . $referinfo_1['user_name'] . '获得,订单金额为' . $order['goods_amount'] . ',1级佣金比例为' . $tuijian_seller_ratio1
                    . ',推荐关系为:' . $seller_info['user_name'] . '<<--' . $referinfo_1['user_name'],
                )
        );
        if(!$mysql_flag){
            return false;
        }
        //1级推荐人获得佣金
        $mysql_flag=$this->_member_mod->change_money(
                array(
                    'user_id' => $referinfo_1['user_id'],
                    'add_time'=>$add_time,
                    'user_name' => $referinfo_1['user_name'],
                    'order_sn' => $order['order_sn'],
                    'type' => ACCOUNT_TYPE_TUIJIAN_SELLER,
                    'money' => round($order['goods_amount'] * $tuijian_seller_ratio1, 2), #一级推荐人应该获取的佣金
                    'money_flow' => 'income', #流入佣金
                    'complete' => '1',
                    'log_text' => '恭喜你获得' . round($order['goods_amount'] * $tuijian_seller_ratio1, 2) . '元佣金,佣金由店铺' . $seller_info['user_name']
                    . '缴纳,订单金额为' . $order['goods_amount'] . ',1级佣金比例为' . $tuijian_seller_ratio1
                    . ',推荐关系为:' . $seller_info['user_name'] . '<<--' . $referinfo_1['user_name'],
                )
        );
        if(!$mysql_flag){
            return false;
        }
    }
        /* 第1级 佣金操作  查看卖家是否有推荐人  END */



        /* 第2级 佣金操作  查看卖家是否有推荐人  BEGIN */
        if (!Conf::get('tuijian_seller_ratio2')) {
            return true;
        }
        $tuijian_seller_ratio2 = round(Conf::get('tuijian_seller_ratio2') / 100, 2);
        //2级推荐人 不存在卖家的推荐人则返回
        if (!$referinfo_1['referid']) {
            return true;
        }
        $referid_2 = $referinfo_1['referid'];
        //查看推荐人是否存在 不存在则不操作
        $referinfo_2 = $this->_member_mod->get($referid_2);
        if (empty($referinfo_2)) {
            return true;
        }
        if(round($order['goods_amount'] * $tuijian_seller_ratio2, 2)>0){
        //卖家先扣除佣金
        $mysql_flag=$this->_member_mod->change_money(
                array(
                    'user_id' => $seller_id,
                    'add_time'=>$add_time,
                    'user_name' => $seller_info['user_name'],
                    'order_sn' => $order['order_sn'],
                    'type' => ACCOUNT_TYPE_TUIJIAN_SELLER,
                    'money' => round($order['goods_amount'] * $tuijian_seller_ratio2, 2), #2级推荐人应该获取的佣金
                    'money_flow' => 'outlay', #流出佣金
                    'complete' => '1',
                    'log_text' => '恭喜你缴纳' . round($order['goods_amount'] * $tuijian_seller_ratio2, 2) . '元佣金,佣金被2级店铺推荐者' . $referinfo_2['user_name']
                    . '获得,订单金额为' . $order['goods_amount'] . ',2级佣金比例为' . $tuijian_seller_ratio2
                    . ',推荐关系为:' . $seller_info['user_name'] . '<<--' . $referinfo_1['user_name'] . '<<--' . $referinfo_2['user_name'],
                )
        );
        if(!$mysql_flag){
            return false;
        }
        //2级推荐人获得佣金
        $mysql_flag=$this->_member_mod->change_money(
                array(
                    'user_id' => $referinfo_2['user_id'],
                    'add_time'=>$add_time,
                    'user_name' => $referinfo_2['user_name'],
                    'order_sn' => $order['order_sn'],
                    'type' => ACCOUNT_TYPE_TUIJIAN_SELLER,
                    'money' => round($order['goods_amount'] * $tuijian_seller_ratio2, 2), #2级推荐人应该获取的佣金
                    'money_flow' => 'income', #流入佣金
                    'complete' => '1',
                    'log_text' => '恭喜你获得' . round($order['goods_amount'] * $tuijian_seller_ratio2, 2) . '元佣金,佣金由店铺' . $seller_info['user_name']
                    . '缴纳,订单金额为' . $order['goods_amount'] . ',2级佣金比例为' . $tuijian_seller_ratio2
                    . ',推荐关系为:' . $seller_info['user_name'] . '<<--' . $referinfo_1['user_name'] . '<<--' . $referinfo_2['user_name'],
                )
        );
        if(!$mysql_flag){
            return false;
        }
    }
        /* 第2级 佣金操作  查看卖家是否有推荐人  END */


        /* 第3级 佣金操作  查看卖家是否有推荐人  BEGIN */
        if (!Conf::get('tuijian_seller_ratio3')) {
            return true;
        }
        $tuijian_seller_ratio3 = round(Conf::get('tuijian_seller_ratio3') / 100, 2);
        //2级推荐人 不存在卖家的推荐人则返回
        if (!$referinfo_2['referid']) {
            return true;
        }
        $referid_3 = $referinfo_2['referid'];
        //查看推荐人是否存在 不存在则不操作
        $referinfo_3 = $this->_member_mod->get($referid_3);
        if (empty($referinfo_3)) {
            return true;
        }
        if(round($order['goods_amount'] * $tuijian_seller_ratio3, 2)>0){
        //卖家先扣除佣金
        $mysql_flag=$this->_member_mod->change_money(
                array(
                    'user_id' => $seller_id,
                    'add_time'=>$add_time,
                    'user_name' => $seller_info['user_name'],
                    'order_sn' => $order['order_sn'],
                    'type' => ACCOUNT_TYPE_TUIJIAN_SELLER,
                    'money' => round($order['goods_amount'] * $tuijian_seller_ratio3, 2), #3级推荐人应该获取的佣金
                    'money_flow' => 'outlay', #流出佣金
                    'complete' => '1',
                    'log_text' => '恭喜你缴纳' . round($order['goods_amount'] * $tuijian_seller_ratio3, 2) . '元佣金,佣金被3级店铺推荐者' . $referinfo_3['user_name']
                    . '获得,订单金额为' . $order['goods_amount'] . ',3级佣金比例为' . $tuijian_seller_ratio3
                    . ',推荐关系为:' . $seller_info['user_name'] . '<<--' . $referinfo_1['user_name'] . '<<--' . $referinfo_2['user_name'] . '<<--' . $referinfo_3['user_name'],
                )
        );
        if(!$mysql_flag){
            return false;
        }
        //3级推荐人获得佣金
        $mysql_flag=$this->_member_mod->change_money(
                array(
                    'user_id' => $referinfo_3['user_id'],
                    'add_time'=>$add_time,
                    'user_name' => $referinfo_3['user_name'],
                    'order_sn' => $order['order_sn'],
                    'type' => ACCOUNT_TYPE_TUIJIAN_SELLER,
                    'money' => round($order['goods_amount'] * $tuijian_seller_ratio3, 2), #3级推荐人应该获取的佣金
                    'money_flow' => 'income', #流入佣金
                    'complete' => '1',
                    'log_text' => '恭喜你获得' . round($order['goods_amount'] * $tuijian_seller_ratio3, 2) . '元佣金,佣金由店铺' . $seller_info['user_name']
                    . '缴纳,订单金额为' . $order['goods_amount'] . ',3级佣金比例为' . $tuijian_seller_ratio3
                    . ',推荐关系为:' . $seller_info['user_name'] . '<<--' . $referinfo_1['user_name'] . '<<--' . $referinfo_2['user_name'] . '<<--' . $referinfo_3['user_name'],
                )
        );
        if(!$mysql_flag){
            return false;
        }
    }
        /* 第3级 佣金操作  查看卖家是否有推荐人  END */
    return  true;
    }
    

    /**
     * 此处是  推荐成为买家， 买家购买产品后，他的推荐者可以获得佣金，佣金来源于卖家
     */
    function tuijian_buyer($order) {
        $add_time = gmtime();
        //判断是否开启 推荐会员成为买家 ， 买家购买产品  推荐者会获取提成，佣金来源于卖家
        if (!Conf::get('tuijian_buyer_status')) {
            return true;
        }
        /* 第1级 佣金操作  查看卖家是否有推荐人  BEGIN */
        

        //卖家相关信息
        $seller_id = $order['seller_id'];
        $seller_info = $this->_member_mod->get($seller_id);

        
        //卖家信息不存在
        if(empty($seller_info)){
            return true;
        }
        
        //买家相关信息
        $buyer_id = $order['buyer_id'];
        $buyer_info = $this->_member_mod->get($buyer_id);


        //1级推荐人 不存在买家的推荐人则返回
        if (!$buyer_info['referid']) {
            return true;
        }
        $referid_1 = $buyer_info['referid'];
        //查看推荐人是否存在 不存在则不操作
        $referinfo_1 = $this->_member_mod->get($referid_1);
        if (empty($referinfo_1)) {
            return true;
        }


        $order_goods_mod = &m('ordergoods');
        $order_goods = $order_goods_mod->find("order_id={$order['order_id']}");

        //获取店铺设置的佣金比例
        $store_mod = &m('store');
        $store = $store_mod->get($seller_id);
        
        
        foreach ($order_goods as $key => $goods) {
            $order_goods[$key]['tuijian_buyer_ratio1'] = Conf::get('tuijian_buyer_ratio1');
            $order_goods[$key]['tuijian_buyer_ratio2'] = Conf::get('tuijian_buyer_ratio2');
            $order_goods[$key]['tuijian_buyer_ratio3'] = Conf::get('tuijian_buyer_ratio3');
            $order_goods[$key]['tuijian_buyer_type1'] = "(系统比例)";
            $order_goods[$key]['tuijian_buyer_type2'] = "(系统比例)";
            $order_goods[$key]['tuijian_buyer_type3'] = "(系统比例)";

            
            
            if ($store['store_tuijian_buyer_ratio1'] > Conf::get('tuijian_buyer_ratio1')) {
                $order_goods[$key]['tuijian_buyer_ratio1'] = $store['store_tuijian_buyer_ratio1'];
                $order_goods[$key]['tuijian_buyer_type1'] = '(店铺比例)';
            }
            if ($store['store_tuijian_buyer_ratio2'] > Conf::get('tuijian_buyer_ratio2')) {
                $order_goods[$key]['tuijian_buyer_ratio2'] = $store['store_tuijian_buyer_ratio2'];
                $order_goods[$key]['tuijian_buyer_type2'] = '(店铺比例)';
            }
            if ($store['store_tuijian_buyer_ratio3'] > Conf::get('tuijian_buyer_ratio3')) {
                $order_goods[$key]['tuijian_buyer_ratio3'] = $store['store_tuijian_buyer_ratio3'];
                $order_goods[$key]['tuijian_buyer_type3'] = '(店铺比例)';
            }
        }

        foreach ($order_goods as $key => $goods) {
            $tuijian_buyer_ratio1 = round($goods['tuijian_buyer_ratio1'] / 100, 2);
            
            $goods_acount = $goods['price']*$goods['quantity'];
            if(round($goods_acount * $tuijian_buyer_ratio1, 2)>0){
            //卖家先扣除佣金
            $mysql_flag=$this->_member_mod->change_money(
                    array(
                        'user_id' => $seller_id,
                        'add_time' => $add_time,
                        'user_name' => $seller_info['user_name'],
                        'order_sn' => $order['order_sn'],
                        'type' => ACCOUNT_TYPE_TUIJIAN_BUYER,
                        'money' => round($goods_acount * $tuijian_buyer_ratio1, 2), #一级推荐人应该获取的佣金
                        'money_flow' => 'outlay', #流出佣金
                        'complete' => '1',
                        'log_text' => '恭喜你缴纳' . round($goods_acount * $tuijian_buyer_ratio1, 2) . '元佣金'.$goods['tuijian_buyer_type1'].',佣金被1级买家推荐者' . $referinfo_1['user_name']
                        . '获得,产品['.$goods['goods_name'].'],价格('.$goods['price'].')X数量('.$goods['quantity'].') = 产品总价('.$goods_acount . '),1级佣金比例为' . $tuijian_buyer_ratio1
                        . ',推荐关系为:' . $referinfo_1['user_name'] . '<<--' .$buyer_info['user_name'] ,
                    )
            );
            if(!$mysql_flag){
                return false;
            }
            //1级推荐人获得佣金
            $mysql_flag=$this->_member_mod->change_money(
                    array(
                        'user_id' => $referinfo_1['user_id'],
                        'add_time' => $add_time,
                        'user_name' => $referinfo_1['user_name'],
                        'order_sn' => $order['order_sn'],
                        'type' => ACCOUNT_TYPE_TUIJIAN_BUYER,
                        'money' => round($goods_acount * $tuijian_buyer_ratio1, 2), #一级推荐人应该获取的佣金
                        'money_flow' => 'income', #流入佣金
                        'complete' => '1',
                        'log_text' => '恭喜你获得' . round($goods_acount * $tuijian_buyer_ratio1, 2) . '元佣金'.$goods['tuijian_buyer_type1'].',佣金由店铺' . $seller_info['user_name']
                        . '缴纳,产品['.$goods['goods_name'].'],价格('.$goods['price'].')X数量('.$goods['quantity'].') = 产品总价('.$goods_acount . '),1级佣金比例为' . $tuijian_buyer_ratio1
                        . ',推荐关系为:' . $referinfo_1['user_name'] . '<<--' .$buyer_info['user_name'] ,
                    )
            );
            if(!$mysql_flag){
                return false;
            }
        }
        }
        /* 第1级 佣金操作  查看卖家是否有推荐人  END */



        /* 第2级 佣金操作  查看买家是否有推荐人  BEGIN */
       
        //2级推荐人 不存在买家的推荐人则返回
        if (!$referinfo_1['referid']) {
            return true;
        }
        $referid_2 = $referinfo_1['referid'];
        //查看推荐人是否存在 不存在则不操作
        $referinfo_2 = $this->_member_mod->get($referid_2);
        if (empty($referinfo_2)) {
            return true;
        }

        foreach ($order_goods as $key => $goods) {
            $tuijian_buyer_ratio2 = round($goods['tuijian_buyer_ratio2'] / 100, 2);
            $goods_acount = $goods['price']*$goods['quantity'];
            if(round($goods_acount * $tuijian_buyer_ratio2, 2)>0){
            //卖家先扣除佣金
            $mysql_flag=$this->_member_mod->change_money(
                    array(
                        'user_id' => $seller_id,
                        'add_time' => $add_time,
                        'user_name' => $seller_info['user_name'],
                        'order_sn' => $order['order_sn'],
                        'type' => ACCOUNT_TYPE_TUIJIAN_BUYER,
                        'money' => round($goods_acount * $tuijian_buyer_ratio2, 2), #2级推荐人应该获取的佣金
                        'money_flow' => 'outlay', #流出佣金
                        'complete' => '1',
                        'log_text' => '恭喜你缴纳' . round($goods_acount * $tuijian_buyer_ratio2, 2) . '元佣金'.$goods['tuijian_buyer_type2'].',佣金被2级买家推荐者' . $referinfo_2['user_name']
                        . '获得,产品['.$goods['goods_name'].'],价格('.$goods['price'].')X数量('.$goods['quantity'].') = 产品总价('.$goods_acount . '),2级佣金比例为' . $tuijian_buyer_ratio2
                        . ',推荐关系为:' . $referinfo_2['user_name'] . '-->>' . $referinfo_1['user_name'] . '-->>' .$buyer_info['user_name'] ,
                    )
            );
            if(!$mysql_flag){
                return false;
            }
            //2级推荐人获得佣金
            $mysql_flag=$this->_member_mod->change_money(
                    array(
                        'user_id' => $referinfo_2['user_id'],
                        'add_time' => $add_time,
                        'user_name' => $referinfo_2['user_name'],
                        'order_sn' => $order['order_sn'],
                        'type' => ACCOUNT_TYPE_TUIJIAN_BUYER,
                        'money' => round($goods_acount * $tuijian_buyer_ratio2, 2), #2级推荐人应该获取的佣金
                        'money_flow' => 'income', #流入佣金
                        'complete' => '1',
                        'log_text' => '恭喜你获得' . round($goods_acount * $tuijian_buyer_ratio2, 2) . '元佣金'.$goods['tuijian_buyer_type2'].',佣金由店铺' . $seller_info['user_name']
                        . '缴纳,产品['.$goods['goods_name'].'],价格('.$goods['price'].')X数量('.$goods['quantity'].') = 产品总价('.$goods_acount . '),2级佣金比例为' . $tuijian_buyer_ratio2
                        . ',推荐关系为:' . $referinfo_2['user_name'] . '-->>' . $referinfo_1['user_name'] . '-->>' .$buyer_info['user_name'] ,
                    )
            );
            if(!$mysql_flag){
                return false;
            }
        }
        }
        /* 第2级 佣金操作  查看卖家是否有推荐人  END */


        /* 第3级 佣金操作  查看卖家是否有推荐人  BEGIN */
        
        $tuijian_buyer_ratio3 = round(Conf::get('tuijian_buyer_ratio3') / 100, 2);
        //2级推荐人 不存在卖家的推荐人则返回
        if (!$referinfo_2['referid']) {
            return true;
        }
        $referid_3 = $referinfo_2['referid'];
        //查看推荐人是否存在 不存在则不操作
        $referinfo_3 = $this->_member_mod->get($referid_3);
        if (empty($referinfo_3)) {
            return true;
        }
        foreach ($order_goods as $key => $goods) {
            $tuijian_buyer_ratio3 = round($goods['tuijian_buyer_ratio3'] / 100, 2);
            $goods_acount = $goods['price']*$goods['quantity'];
            if(round($goods_acount * $tuijian_buyer_ratio3, 2)>0){
            //卖家先扣除佣金
            $mysql_flag=$this->_member_mod->change_money(
                    array(
                        'user_id' => $seller_id,
                        'add_time' => $add_time,
                        'user_name' => $seller_info['user_name'],
                        'order_sn' => $order['order_sn'],
                        'type' => ACCOUNT_TYPE_TUIJIAN_BUYER,
                        'money' => round($goods_acount * $tuijian_buyer_ratio3, 2), #3级推荐人应该获取的佣金
                        'money_flow' => 'outlay', #流出佣金
                        'complete' => '1',
                        'log_text' => '恭喜你缴纳' . round($goods_acount * $tuijian_buyer_ratio3, 2) . '元佣金'.$goods['tuijian_buyer_type3'].',佣金被3级买家推荐者' . $referinfo_3['user_name']
                        . '获得,产品['.$goods['goods_name'].'],价格('.$goods['price'].')X数量('.$goods['quantity'].') = 产品总价('.$goods_acount . '),3级佣金比例为' . $tuijian_buyer_ratio3
                        . ',推荐关系为:' . $referinfo_3['user_name'] . '-->>' . $referinfo_2['user_name'] . '-->>' . $referinfo_1['user_name'] . '-->>' . $buyer_info['user_name'],
                    )
            );
            if(!$mysql_flag){
                return false;
            }
            //3级推荐人获得佣金
            $mysql_flag=$this->_member_mod->change_money(
                    array(
                        'user_id' => $referinfo_3['user_id'],
                        'add_time' => $add_time,
                        'user_name' => $referinfo_3['user_name'],
                        'order_sn' => $order['order_sn'],
                        'type' => ACCOUNT_TYPE_TUIJIAN_BUYER,
                        'money' => round($goods_acount * $tuijian_buyer_ratio3, 2), #3级推荐人应该获取的佣金
                        'money_flow' => 'income', #流入佣金
                        'complete' => '1',
                        'log_text' => '恭喜你获得' . round($goods_acount * $tuijian_buyer_ratio3, 2) . '元佣金'.$goods['tuijian_buyer_type3'].',佣金由店铺' . $seller_info['user_name']
                        . '缴纳,产品['.$goods['goods_name'].'],价格('.$goods['price'].')X数量('.$goods['quantity'].') = 产品总价('.$goods_acount . '),3级佣金比例为' . $tuijian_buyer_ratio3
                        . ',推荐关系为:' . $referinfo_3['user_name'] . '-->>' . $referinfo_2['user_name'] . '-->>' . $referinfo_1['user_name'] . '-->>' . $buyer_info['user_name'],
                    )
            );
            if(!$mysql_flag){
                return false;
            }
        }
        }
        /* 第3级 佣金操作  查看卖家是否有推荐人  END */
        return  true;
    }

    /**
     * 此处是  推荐成为买家， 买家购买产品后，他的推荐者可以获得佣金，佣金来源于卖家
     */
    function tuijian_buyer_bak($order) {
        $add_time = gmtime();
        //判断是否开启 推荐会员成为买家 ， 买家购买产品  推荐者会获取提成，佣金来源于卖家
        if (!Conf::get('tuijian_buyer_status')) {
            return true;
        }
        /* 第1级 佣金操作  查看卖家是否有推荐人  BEGIN */
        if (!Conf::get('tuijian_buyer_ratio1')) {
            return true;
        }
        $tuijian_buyer_ratio1 = round(Conf::get('tuijian_buyer_ratio1') / 100, 2);

        //卖家相关信息
        $seller_id = $order['seller_id'];
        $seller_info = $this->_member_mod->get($seller_id);

        //买家相关信息
        $buyer_id = $order['buyer_id'];
        $buyer_info = $this->_member_mod->get($buyer_id);


        //1级推荐人 不存在买家的推荐人则返回
        if (!$buyer_info['referid']) {
            return true;
        }
        $referid_1 = $buyer_info['referid'];
        //查看推荐人是否存在 不存在则不操作
        $referinfo_1 = $this->_member_mod->get($referid_1);
        if (empty($referinfo_1)) {
            return true;
        }
        //卖家先扣除佣金
        $mysql_flag=$this->_member_mod->change_money(
                array(
                    'user_id' => $seller_id,
                    'add_time'=>$add_time,
                    'user_name' => $seller_info['user_name'],
                    'order_sn' => $order['order_sn'],
                    'type' => ACCOUNT_TYPE_TUIJIAN_BUYER,
                    'money' => round($order['goods_amount'] * $tuijian_buyer_ratio1, 2), #一级推荐人应该获取的佣金
                    'money_flow' => 'outlay', #流出佣金
                    'complete' => '1',
                    'log_text' => '恭喜你缴纳' . round($order['goods_amount'] * $tuijian_buyer_ratio1, 2) . '元佣金,佣金被1级买家推荐者' . $referinfo_1['user_name']
                    . '获得,订单金额为' . $order['goods_amount'] . ',1级佣金比例为' . $tuijian_buyer_ratio1
                    . ',推荐关系为:' . $buyer_info['user_name'] . '<<--' . $referinfo_1['user_name'],
                )
        );
        //1级推荐人获得佣金
        $mysql_flag=$this->_member_mod->change_money(
                array(
                    'user_id' => $referinfo_1['user_id'],
                    'add_time'=>$add_time,
                    'user_name' => $referinfo_1['user_name'],
                    'order_sn' => $order['order_sn'],
                    'type' => ACCOUNT_TYPE_TUIJIAN_BUYER,
                    'money' => round($order['goods_amount'] * $tuijian_buyer_ratio1, 2), #一级推荐人应该获取的佣金
                    'money_flow' => 'income', #流入佣金
                    'complete' => '1',
                    'log_text' => '恭喜你获得' . round($order['goods_amount'] * $tuijian_buyer_ratio1, 2) . '元佣金,佣金由店铺' . $seller_info['user_name']
                    . '缴纳,订单金额为' . $order['goods_amount'] . ',1级佣金比例为' . $tuijian_buyer_ratio1
                    . ',推荐关系为:' . $buyer_info['user_name'] . '<<--' . $referinfo_1['user_name'],
                )
        );
        /* 第1级 佣金操作  查看卖家是否有推荐人  END */



        /* 第2级 佣金操作  查看买家是否有推荐人  BEGIN */
        if (!Conf::get('tuijian_buyer_ratio2')) {
            return true;
        }
        $tuijian_buyer_ratio2 = round(Conf::get('tuijian_buyer_ratio2') / 100, 2);
        //2级推荐人 不存在买家的推荐人则返回
        if (!$referinfo_1['referid']) {
            return true;
        }
        $referid_2 = $referinfo_1['referid'];
        //查看推荐人是否存在 不存在则不操作
        $referinfo_2 = $this->_member_mod->get($referid_2);
        if (empty($referinfo_2)) {
            return true;
        }
        //卖家先扣除佣金
        $mysql_flag=$this->_member_mod->change_money(
                array(
                    'user_id' => $seller_id,
                    'add_time'=>$add_time,
                    'user_name' => $seller_info['user_name'],
                    'order_sn' => $order['order_sn'],
                    'type' => ACCOUNT_TYPE_TUIJIAN_BUYER,
                    'money' => round($order['goods_amount'] * $tuijian_buyer_ratio2, 2), #2级推荐人应该获取的佣金
                    'money_flow' => 'outlay', #流出佣金
                    'complete' => '1',
                    'log_text' => '恭喜你缴纳' . round($order['goods_amount'] * $tuijian_buyer_ratio2, 2) . '元佣金,佣金被2级买家推荐者' . $referinfo_2['user_name']
                    . '获得,订单金额为' . $order['goods_amount'] . ',2级佣金比例为' . $tuijian_buyer_ratio2
                    . ',推荐关系为:' . $buyer_info['user_name'] . '<<--' . $referinfo_1['user_name'] . '<<--' . $referinfo_2['user_name'],
                )
        );
        //2级推荐人获得佣金
        $mysql_flag=$this->_member_mod->change_money(
                array(
                    'user_id' => $referinfo_2['user_id'],
                    'add_time'=>$add_time,
                    'user_name' => $referinfo_2['user_name'],
                    'order_sn' => $order['order_sn'],
                    'type' => ACCOUNT_TYPE_TUIJIAN_BUYER,
                    'money' => round($order['goods_amount'] * $tuijian_buyer_ratio2, 2), #2级推荐人应该获取的佣金
                    'money_flow' => 'income', #流入佣金
                    'complete' => '1',
                    'log_text' => '恭喜你获得' . round($order['goods_amount'] * $tuijian_buyer_ratio2, 2) . '元佣金,佣金由店铺' . $seller_info['user_name']
                    . '缴纳,订单金额为' . $order['goods_amount'] . ',2级佣金比例为' . $tuijian_buyer_ratio2
                    . ',推荐关系为:' . $buyer_info['user_name'] . '<<--' . $referinfo_1['user_name'] . '<<--' . $referinfo_2['user_name'],
                )
        );
        /* 第2级 佣金操作  查看卖家是否有推荐人  END */


        /* 第3级 佣金操作  查看卖家是否有推荐人  BEGIN */
        if (!Conf::get('tuijian_buyer_ratio3')) {
            return true;
        }
        $tuijian_buyer_ratio3 = round(Conf::get('tuijian_buyer_ratio3') / 100, 2);
        //2级推荐人 不存在卖家的推荐人则返回
        if (!$referinfo_2['referid']) {
            return true;
        }
        $referid_3 = $referinfo_2['referid'];
        //查看推荐人是否存在 不存在则不操作
        $referinfo_3 = $this->_member_mod->get($referid_3);
        if (empty($referinfo_3)) {
            return true;
        }
        //卖家先扣除佣金
        $mysql_flag=$this->_member_mod->change_money(
                array(
                    'user_id' => $seller_id,
                    'add_time'=>$add_time,
                    'user_name' => $seller_info['user_name'],
                    'order_sn' => $order['order_sn'],
                    'type' => ACCOUNT_TYPE_TUIJIAN_BUYER,
                    'money' => round($order['goods_amount'] * $tuijian_buyer_ratio3, 2), #3级推荐人应该获取的佣金
                    'money_flow' => 'outlay', #流出佣金
                    'complete' => '1',
                    'log_text' => '恭喜你缴纳' . round($order['goods_amount'] * $tuijian_buyer_ratio3, 2) . '元佣金,佣金被3级买家推荐者' . $referinfo_3['user_name']
                    . '获得,订单金额为' . $order['goods_amount'] . ',3级佣金比例为' . $tuijian_buyer_ratio3
                    . ',推荐关系为:' . $buyer_info['user_name'] . '<<--' . $referinfo_1['user_name'] . '<<--' . $referinfo_2['user_name'] . '<<--' . $referinfo_3['user_name'],
                )
        );
        //3级推荐人获得佣金
        $mysql_flag=$this->_member_mod->change_money(
                array(
                    'user_id' => $referinfo_3['user_id'],
                    'add_time'=>$add_time,
                    'user_name' => $referinfo_3['user_name'],
                    'order_sn' => $order['order_sn'],
                    'type' => ACCOUNT_TYPE_TUIJIAN_BUYER,
                    'money' => round($order['goods_amount'] * $tuijian_buyer_ratio3, 2), #3级推荐人应该获取的佣金
                    'money_flow' => 'income', #流入佣金
                    'complete' => '1',
                    'log_text' => '恭喜你获得' . round($order['goods_amount'] * $tuijian_buyer_ratio3, 2) . '元佣金,佣金由店铺' . $seller_info['user_name']
                    . '缴纳,订单金额为' . $order['goods_amount'] . ',3级佣金比例为' . $tuijian_buyer_ratio3
                    . ',推荐关系为:' . $buyer_info['user_name'] . '<<--' . $referinfo_1['user_name'] . '<<--' . $referinfo_2['user_name'] . '<<--' . $referinfo_3['user_name'],
                )
        );
        /* 第3级 佣金操作  查看卖家是否有推荐人  END */
    }
}
